var jCategoryTree;
$j(document).ready(function () {
	//验证
	applyValidate($("lowestPrice"),"price");
	applyValidate($("highestPrice"),"price");
	
});

$j.fn.extend({
	loading2:function(){
		return this.each(function(){
			$j(this).html('<td colspan="9"><div class="loading_box"><div class="progress_bar"><img src="'+__ctxPath+'/images/icon/loadingBig.gif"/>loading...</div></div></td>');			
		});
	}
});

//显示操作按钮
function displayListButtons(status){
	if(status == null){
		//case: all
		enableIconBtn('multiDelete');
	}else if(status == 1){
		//case:激活
		enableIconBtn('multiDelete');
	}else if(status == 0){
		//case:不激活
		enableIconBtn('multiDelete');
	}else if(status == -1){
		//case:逻辑删除
		disableIconBtn('multiDelete');
	}
}
//显示 隐藏全部
function toggleScreenAll(linkDOM){
	if($j(linkDOM).hasClass("expand")){
		$j(linkDOM).removeClass("expand").addClass("shrink");
		$j('span.toggleScreen').each(function(){
			var jTrThis = $j(this).parents('tr:first');
			var jTrFull = jTrThis.next();
			var productId = jTrThis.attr("productId");
			jTrFull.show();
			if(!jTrFull.hasClass("hasLoad")){
				jTrFull.addClass("hasLoad");
				jTrFull.loading2().load(__ctxPath+'/sales/gift/blank.html?doAction=showProductItem&giftId='+giftId);
			}
			$j(this).removeClass("expand").addClass("shrink");
		});
	
	}else{
		$j(linkDOM).removeClass("shrink").addClass("expand");
		$j('span.toggleScreen').each(function(){
			var jTrThis = $j(this).parents('tr:first');
			var jTrFull = jTrThis.next();
			jTrFull.hide();
			$j(this).removeClass("shrink").addClass("expand");
		});
	}
}	

//显示 隐藏详细
function toggleScreen(linkDOM){
	var jTrThis = $j(linkDOM).parents('tr:first');
	var jTrFull = jTrThis.next();
	var productId = jTrThis.attr("giftId");
	if(jTrFull.is(":hidden")){
		jTrFull.show();
		$j(linkDOM).removeClass("expand").addClass("shrink");
		
		if(!jTrFull.hasClass("hasLoad")){
			jTrFull.addClass("hasLoad");
			jTrFull.loading2().load(__ctxPath+'/sales/gift/blank.html?doAction=showProductItem&giftId='+giftId);
		}
	}else{
		jTrFull.hide();
		$j(linkDOM).removeClass("shrink").addClass("expand");
	}
	
}

//点击树节点动作
function clickTreeItemAction(){
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	searchGifts(true,{'categoryPath':treeItem.categoryPath});
}

//创建产品
function createGift(){
	var url = __ctxPath+"/sales/gift/ajaxPage.html?doAction=add&direct=false";
	if(jCategoryTree.getSelected().length != 0){
		var treeItem = getCategoryItemJSON(jCategoryTree.getSelected());
		url+= "&giftCategoryId="+treeItem.id;
	}
	showGiftForm(url,null);
}


//编辑产品
function editGift(giftId){
	var url = __ctxPath+"/sales/gift/ajaxPage.html?doAction=edit&direct=false&giftId="+giftId;
	showGiftForm(url,null);
}

function showSelectTypeForm(url,param){
	$j('#mainDiv').hide();
	$j('#formDiv').show().loading().load(url,param);
}

//在本页以ajaxPage方式打开productForm 
function showGiftForm(url,param){
	setCurrListHeight();
	//在AJAX调用前初始化编辑器;并在回调函数使用KE.create()方法创建编辑器
	initHtmlEditor('fullDescription',600,300);		
	$j('#mainDiv').hide();
	$j('#formDiv').show().loading().load(url,param,loadHtmlEditor);
}
/*******************************************
1.ajax请求的页面中，在发起AJAX请求前，初始化编辑器[KE.init({})]，否则无法使用编辑器
2.ajax返回HTML后，在回调函数里执行KE.create()创建编辑器
3.详见/StoreAdmin/web/scripts/kindeditor/kindeditor-init.js
*******************************************/
function loadHtmlEditor(){
	createHtmlEditor('fullDescription');	
}

//删除多个产品
function deleteGifts(){
	var paraData=$j("#giftListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var itemNames = fnGetSelectedItemNames();
		if(confirm( __FMT.common_message_confirmDeleteThis+ " " + itemNames+"?")){
			$j.post(__ctxPath + "/sales/gift/blank.html?doAction=multiDelete", paraData, deleteGiftsCallback,"json");
		}
	}else{
		alert(__FMT.giftList_selectGift);
	}
}
//删除产品后的回调函数
function deleteGiftsCallback(data){
	if(data.deleteInfos){
		var deleteSuccessGiftNames = new Array();
		var deleteFalseGiftNames = new Array();
		var deleteLogicalGiftNames = new Array();
		for(var i=0; i< data.deleteInfos.length; i++){
			var deleteInfo = data.deleteInfos[i];
			if(deleteInfo.deleteStatus == 0){
				//case:有正在进行中的订单，删除失败
				deleteFalseGiftNames.push(deleteInfo.giftName);
			}else{
				//case:删除成功
				deleteSuccessGiftNames.push(deleteInfo.giftName);
			}
		}
		deleteSuccessGiftNames.reverse();
		deleteFalseGiftNames.reverse();
		deleteLogicalGiftNames.reverse();
		var deleteString = "";
		var deleteGiftNameString = "";
		for(var i=0; i< deleteSuccessGiftNames.length; i++){
			deleteGiftNameString += deleteSuccessGiftNames[i];
			if(i!=deleteSuccessGiftNames.length-1){deleteGiftNameString+=",";}
		}
		if(deleteSuccessGiftNames.length!=0){
			deleteString += __FMT.gift_label + deleteGiftNameString + __FMT.gift_delete_success + "\r\n";
			sysMsg(deleteString);
		}	
		
		deleteString = "";
		deleteGiftNameString = "";
		for(var i=0; i< deleteFalseGiftNames.length; i++){
			deleteGiftNameString += deleteFalseGiftNames[i];
			if(i!=deleteFalseGiftNames.length-1){deleteGiftNameString+=",";}
		}
		if(deleteFalseGiftNames.length!=0){
			deleteString += __FMT.gift_label + deleteGiftNameString + __FMT.gift_delete_false + "\r\n";
			sysMsg(deleteString,true);
		}
		
		deleteString = "";	
		deleteGiftNameString = "";
		for(var i=0; i< deleteLogicalGiftNames.length; i++){
			deleteGiftNameString += deleteLogicalGiftNames[i];
			if(i!=deleteLogicalGiftNames.length-1){deleteGiftNameString+=",";}
		}
		if(deleteLogicalGiftNames.length!=0){
			deleteString += __FMT.gift_label + deleteGiftNameString + __FMT.gift_delete_logical;
			sysMsg(deleteString);
		}	
	
	}
	refreshGiftList();
}

//联合批修改
function associatedBulkEdit(){
	var paraData=$j("#giftResults :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var productIds="";
		$j.each(paraData,function(i,n){
			productIds += n.value+",";
		});
		productIds = productIds.substring(0,productIds.length-1);
		var url = __ctxPath+"/sales/gift.html?doAction=associatedBulkEdit&productIds="+productIds;
		window.location = url;
	}else{
		alert(__FMT.giftList_selectGift);
	}
}

//统一批修改
function unitiveBulkEdit(){
	var paraData=$j("#giftResults :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var giftIds="";
		$j.each(paraData,function(i,n){
			giftIds += n.value+",";
		});
		giftIds = giftIds.substring(0,giftIds.length-1);
		var url = __ctxPath+"/sales/gift.html?doAction=unitiveBulkEdit&giftIds="+giftIds;
		window.location = url;
	}else{
		alert(__FMT.giftList_selectGift);
	}
}


//刷新产品列表
function refreshGiftList(){
	searchGifts(false);
}
//分页
function fnOnGoToPage(){
	refreshGiftList();
}

//对已有结果进行排序,ascValue表示升序时对应的值,descValue表示降序时对应的值
function sortField(linkDOM,ascValue,descValue){
	var jThDOM = $j(linkDOM).parent('th');
	if(jThDOM.hasClass("order1")){
		$j('#storageOrderby').val(descValue);
	}else{
		$j('#storageOrderby').val(ascValue);
	}
	searchGifts(false);
}

//搜索产品 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchGifts(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#giftSearchCriteria :input').serializeArray();
			    url=__ctxPath+"/sales/gift/blank.html?doAction=search&btnSearch=1";
				$j("#giftResults").loading().load(url,postData);
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			//有搜索结果搜索
			if(param.status  != undefined){
				$j('#storageStatus').val(""+param.status);
			}
			if(param.categoryId  != undefined){
				$j('#storageCategoryId').val(""+param.categoryId);
			}
			if(param.categoryPath  != undefined){
				$j('#storageCategoryPath').val(""+param.categoryPath);
			}
		    postData = $j('#giftResults :input').serializeArray();
		    url=__ctxPath+"/sales/gift/blank.html?doAction=search&btnSearch=1";
			$j("#giftResults").loading().load(url,postData);  
		}    
	}else{
		postData = $j('#giftResults :input').serializeArray();
		url=__ctxPath+"/sales/gift/blank.html?&doAction=search";
		$j("#giftResults").loading().load(url,postData);  
	} 
}



//验证函数
function validateSearchCriteria(){
	if(!validateForm($j('#giftSearchCriteria').get(0))){
    	return false;
   	}
   	return true;
}

//重设搜索条件
function resetSearchCriteria(){
	fnResetForm("giftSearchCriteria");
}


